﻿using System;
using System.Collections.Generic;
using System.Text;

using Obsidian.Action;
using Obsidian.Utils;
using Oak.Base;
using Oak.Model;
using Oak.MagSys.Logic;

namespace Oak.MagSys.Serv
{
    public class AdminMenuItemServ : OakServ<ISession>
    {

        private const string FM_ADMIN_MENU_ITEM = "itemId,adminId,menuItemId,created";
        private const string FN_MENU_ITEM = "itemId,parentId,frameId,text,isSystemMenuItem,sortOrder";

        public AdminMenuItemServ(ISession session) : base(session) { }

        public void Update(ActionRequest req, ActionResponse res)
        {

            ActReqParam param = req.GetParamByNameOrFirst("params");
            if (param == null)
            {
                res.Error("参数item错误");
                return;
            }

            long adminId;
            if (!param.TryGetFirstLong("adminId", out adminId))
            {
                res.Error("参数item属性adminId错误");
                return;
            }

            string strMenuItemIds;
            if (!param.TryGetFirstString("menuItemIds", out strMenuItemIds))
            {
                res.Error("参数item属性menuItemIds错误");
                return;
            }
            long[] menuItemIds = ArrayUtil.SplitInt64(strMenuItemIds, ',');
            AdminMenuItemLG itemLG = new AdminMenuItemLG(Session);
            string msg;
            bool succ = itemLG.UpdateAdminMenuItemsByAdminId(adminId, menuItemIds, out msg);
            if (!succ)
            {
                res.Error(msg);
                return;
            }

        }

        public void List(ActionRequest req, ActionResponse res)
        {
            MsAdminMenuItemQry qry = req.GetModelByNameOrFirst<MsAdminMenuItemQry>("qry");

            AdminMenuItemLG itemLG = new AdminMenuItemLG(Session);
            string msg;
            List<MsAdminMenuItemInfo> list = itemLG.GetAdminMenuItemList(qry, out msg);
            if (list == null)
            {
                res.Error(msg);
                return;
            }

            res.AddResult("items", FM_ADMIN_MENU_ITEM, list)
                .SetRelation("menuItem", "itemId", "menuItems", "itemId");
            res.AddResult("menuItems", FN_MENU_ITEM, "itemId", "menuItem", list);

        }

        public void Listing(ActionRequest req, ActionResponse res)
        {
            AdminMenuItemLG itemLG = new AdminMenuItemLG(Session);
            string msg;
            List<MsMenuItemInfo> list = itemLG.ListingAdminMenuItems(out msg);
            if (list == null)
            {
                res.Error(msg);
                return;
            }

            res.AddResult("menuItems", FN_MENU_ITEM, list);
        }



    }
}
